package com.wuhanins.insmytestdemo.app.launch;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.hjq.toast.ToastUtils;
import com.wuhanins.core.promise.JPromiser;
import com.wuhanins.core.promise.JPromiserQueue;
import com.wuhanins.core.promise.Promiser;
import com.wuhanins.insmytestdemo.BuildConfig;
import com.wuhanins.insmytestdemo.R;
import com.wuhanins.insmytestdemo.app.activity.BDLocationActivity;
import com.wuhanins.zero2.databinding.Insv2FragmentZeroSplashBinding;
import com.wuhanins.zero2.launcher.ZeroSplashFragment;

import java.util.ArrayList;

/**
 * Author  ： logan
 * Time    ： 10/12/21 3:48 PM
 * Desc    ：
 */
public class SplashFragment extends ZeroSplashFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, Insv2FragmentZeroSplashBinding.class);
    }

    @Override
    protected void iView(View view) {
        Glide
                .with(this)
                .load(R.mipmap.splash)
                .centerCrop()
                .into(getBinding().imgSplash);
    }

    @Override
    protected void loading(long millis) {
        long startTime = System.currentTimeMillis();

//            ArrayList<JPromiser> task = MainDomainService.MainSyncData(getContext());
        ArrayList<JPromiser> task = new ArrayList<>();
        task.add(0, new Promiser() {
            @Override
            protected void aync(Deffered<String, String> d) {
                ToastUtils.show("正在加载菜单");
                d.resolve();
            }
        });
        task.add(new Promiser() {
            @Override
            protected void aync(Deffered<String, String> d) {

                long hasBeenRunMillis = millis - (System.currentTimeMillis() - startTime);

                if (hasBeenRunMillis > 0) {
                    SplashFragment.super.loading(hasBeenRunMillis);
                } else {
                    onTimer();
                }
                d.resolve();
            }
        });

        JPromiserQueue.newInstance().sync(task).error(result -> {
            onTimer();
        }).run();
    }

    @Override
    protected void onTimer() {
//        LocationActivity.start(requireContext());
        BDLocationActivity.start(requireContext());
//        MainActivity.start(requireContext());
        if (!BuildConfig.DEBUG) {
            getActivity().finish();
        }
    }
}